#include "stdafx.h"
#include "XComboImpl.h"

#include "Util.h"
#include "UIUtil.h"

#include "IntSkinImpl.h"
#include "XBSTR.h"

template <class I>
CXComboImpl<I>::CXComboImpl()
{
    ModifyStyle(WS_TABSTOP, 0);
}

template <class I>
CXComboImpl<I>::~CXComboImpl()
{
}

//////////////////////////////////////////////////////////////////////////
// IXCombo
template <class I>
int CXComboImpl<I>::get_ItemCount()
{
    if(m_xRealWnd != NULL)
        return ::SendMessage((HWND)m_xRealWnd, CB_GETCOUNT, 0, 0);
    return -1;
}

template <class I>
int CXComboImpl<I>::get_CurSel()
{
    if(m_xRealWnd != NULL)
        return ::SendMessage((HWND)m_xRealWnd, CB_GETCURSEL, 0, 0);
    return -1;
}

template <class I>
void CXComboImpl<I>::put_CurSel(IN int nCurSel)
{
    if(m_xRealWnd != NULL)
        ::SendMessage((HWND)m_xRealWnd, CB_SETCURSEL, (WPARAM)nCurSel, 0);
}

template <class I>
int CXComboImpl<I>::get_MinVisibleItemCount()
{
    if(m_xRealWnd != NULL)
        return ::SendMessage((HWND)m_xRealWnd, CB_GETMINVISIBLE, 0, 0);
    return -1;
}

template <class I>
void CXComboImpl<I>::put_MinVisibleItemCount(IN int nCount)
{
    if(m_xRealWnd != NULL)
        ::SendMessage((HWND)m_xRealWnd, CB_SETMINVISIBLE, (WPARAM)nCount, 0);
}

template <class I>
int CXComboImpl<I>::AddString(IN LPCTSTR szText)
{
    if(m_xRealWnd != NULL)
    {
        XString strText, strTextResId;
        IntSkinImpl::SetorLoadStringSaveResId(szText, strText, strTextResId);
        return ::SendMessage((HWND)m_xRealWnd, CB_ADDSTRING, 0, (LPARAM)strText.GetData());
    }
    return -1;
}

template <class I>
int CXComboImpl<I>::InsertString(IN int uIndex, LPCTSTR szText)
{
    if(m_xRealWnd != NULL)
    {
        XString strText, strTextResId;
        IntSkinImpl::SetorLoadStringSaveResId(szText, strText, strTextResId);
        return ::SendMessage((HWND)m_xRealWnd, CB_INSERTSTRING, (WPARAM)uIndex, (LPARAM)strText.GetData());
    }
    return -1;
}

template <class I>
int CXComboImpl<I>::DeleteString(IN int uIndex)
{
    if(m_xRealWnd != NULL)
        return ::SendMessage((HWND)m_xRealWnd, CB_DELETESTRING, (WPARAM)uIndex, 0);
    return -1;
}

template <class I>
DWORD CXComboImpl<I>::get_ItemData(IN int nIndex)
{
    if(m_xRealWnd != NULL)
        return ::SendMessage((HWND)m_xRealWnd, CB_GETITEMDATA, (WPARAM)nIndex, 0);
    return -1;
}

template <class I>
void CXComboImpl<I>::put_ItemData(IN int nIndex, IN DWORD dwItemData)
{
    if(m_xRealWnd != NULL)
        ::SendMessage((HWND)m_xRealWnd, CB_DELETESTRING, (WPARAM)nIndex, (LPARAM)dwItemData);
}

//////////////////////////////////////////////////////////////////////////
// CXHwndImpl Callback
template <class I>
XString CXComboImpl<I>::GetHwndClassName()
{
    return XString(WC_COMBOBOX);
}

// CXControlImpl
template <class I>
HRESULT CXComboImpl<I>::OnSkinLangChanged()
{
    XASSERT_ERROR(FALSE, _T("Combo OnSkinLoadChanged Not Impl"));
    return S_OK;
}
//////////////////////////////////////////////////////////////////////////

template <class I>
DWORD CXComboImpl<I>::GetHwndFlags()
{
    return XHwnd_NeedCommand;
}

template <class I>
BOOL CXComboImpl<I>::OnCommand(UINT command)
{
    LPCTSTR szEvt = NULL;
    if(command == CBN_SELCHANGE)
        szEvt = ComboEvent_SelChange;

    if(szEvt != NULL)
    {
        InvokeEventHandler(this, szEvt, (LPARAM)m_xRealWnd);
        return TRUE;
    }

    return FALSE;
}

template <class I>
HRESULT CXComboImpl<I>::get_Text(IN BSTR* bstrText)
{
    HRESULT hResult = superclass::get_Text(bstrText);
    return hResult;
}
